<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Index_Home extends CI_Controller {
	/**
	 /system/core/Common/error
	 $da = error(4001);  返回错误提示  
	 echo _echo($da);   等于 echo json_encode($da, JSON_UNESCAPED_UNICODE);
	 
	 /application/libraries/Mobile_index_service.php  加载 对应 
	 $this->load->library('mobile_index_service');  
	 
	*/

	public function __construct(){
		parent::__construct();
		header('Content-Type: application/json; charset=utf-8');
		$this->load->model('Model_m'); //增删改查 基本类
		$this->load->library('api/mobile_index_service');  
	}	
	
	public function user_api()
	{	
		$server_ip = $_SERVER["REMOTE_ADDR"]; //获取到的IP地址 $output = isset($value) ? $value : 'No value set.';
    	$appid = isset($_REQUEST['appid'])?$_REQUEST['appid']:'';
    	
    	$ip_where = array('appid'=>$appid,'ip'=>$server_ip); //where
    	//判断IP地址是否合法  这里用户只跟指定IP交互用户数据
    	$user_IP = $this->Model_m->select('app_auth',$ip_where,'id desc','1,1');
    		
    	if($user_IP){
			if($server_ip != $user_IP[0]['ip']){
	    		$data['error'] = error(4003);
	    		
	    		echo json_encode($data,JSON_UNESCAPED_UNICODE);
		    	exit;
			}	

	    	if($appid != $user_IP[0]['appid']){
	    		$data['error'] = $this->Model_m->data_error('4003');
		    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		    	exit;
			}	
    	}else{
    		$data['error'] = $this->Model_m->data_error('4001');
	    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
	    	exit;
    	}

		switch ($_SERVER["REQUEST_METHOD"]) {
			case 'GET':
				$data['appid'] = $this->input->get('appid');  //获取到的IP地址 
				$data['server_ip'] = $_SERVER["REMOTE_ADDR"]; 

				echo json_encode($ad_index);
				break;
			case 'POST':
				
				break;
			case 'PUT':
				$data['act'] = $this->input->get('act');   //首页广告
				$data['pid'] = $this->input->get('pid');  //广告位
				$data['limt'] = $this->input->get('limt'); //数量	
				$ad_index = $this->mobile_index_service->put_index($data);
				
				echo json_encode($ad_index);
				break;
			case 'DELETE':
				$data['act'] = $this->input->get('act');   //首页广告
				$data['pid'] = $this->input->get('pid');  //广告位
				$data['limt'] = $this->input->get('limt'); //数量	
				$ad_index = $this->mobile_index_service->delete_index($data);
				
				echo json_encode($ad_index);
				break;

			default:
				# code...
				break;
		}
die;
		
	
		if (isset($_REQUEST['act']) == 'article') {
			$act = $this->input->get('article');   //首页文章
			$cat_id = $this->input->get('cat_id');   //文章类型
			$limt = $this->input->get('limt');   //数量	

			$article_where = array('is_open'=>1,'cat_id'=>$cat_id); //cat_id=1  文章类别  例：=1新手上路
			$article_index = $this->Model_m->select('article',$article_where,'article_id asc',$limt); //article_index_1  文章_首页_文章类型
			
			echo json_encode($article_index, JSON_UNESCAPED_UNICODE);
		}


		//$this->load->view('mobile/index.html');
	}
}

